//
//  ViewController.m
//  18--Homework
//
//  Created by 冀永金 on 16/8/8.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import "Contact.h"
#import "ContactTableViewCell.h"
#import "DataBaseHandler.h"
#import "VCOfThird.h"
#import "ReadandSaveCaches.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableViewOfContact;
//@property (nonatomic, strong) NSMutableArray *marrData;
@property (nonatomic, strong) NSMutableArray *marrContact;
@property (nonatomic, strong) UIImageView *image;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"通讯录";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(handleNext)];
    self.marrContact = [NSMutableArray array];
    self.marrContact = [[DataBaseHandler shareManager]selectAll].mutableCopy;
    //[self.tableViewOfContact reloadData];
    [self createTabelView];
   
    
   // [_tableViewOfContact reloadData];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.marrContact = [[DataBaseHandler shareManager]selectAll].mutableCopy;
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject] stringByAppendingPathComponent:@"path"];
    NSLog(@"%@",NSHomeDirectory());
    NSError *error = nil;
    [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
    
    NSString *documents = [path stringByAppendingPathComponent:@"image"];

    NSData *imageNewData = [NSData dataWithContentsOfFile:documents];
    _image.image = [UIImage imageWithData:imageNewData];
      [_tableViewOfContact reloadData];
    
}
- (void)createTabelView {
    self.tableViewOfContact = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableViewOfContact];
    self.tableViewOfContact.delegate = self;
    self.tableViewOfContact.dataSource = self;
    [_tableViewOfContact registerClass:[ContactTableViewCell  class] forCellReuseIdentifier:@"pool"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.marrContact.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   ContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    Contact *contact = self.marrContact[indexPath.row];
    [cell setValuesWithContact:contact];
    cell.image.image = _image.image;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
- (void)handleNext {
    VCOfThird *third = [[VCOfThird alloc]init];
    [self.navigationController pushViewController:third animated:YES];
    //third.delegate =self;
   
}
- (void)addContact:(NSMutableDictionary *)dic {
//    [self.marrContact addObject:dic];
//    Contact *contact = [[Contact alloc]init];
//        
//    [self.tableViewOfContact reloadData];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
